package com.yinhe.music.yhmusic.utils;

import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.yinhe.music.common.utils.ScreenUtils;

public class KeyboardUtil {

    /*
     * 显示键盘
     * */
    public static void showKeyboard(Activity activity, View view) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(activity.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view, 0);
    }

    /*
     * 隐藏键盘
     * */
    public static void hideKeyboard(Activity activity) {
        if (activity == null) return;
        final View v = activity.getWindow().peekDecorView();
        if (v != null && v.getWindowToken() != null) {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(
                    activity.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }

    /**
     * @param rootView
     * @return b
     * 判断键盘弹出状态
     */
    public static boolean isKeyboardShown(View rootView) {
        Rect r = new Rect();
        rootView.getWindowVisibleDisplayFrame(r);
        if (ScreenUtils.getScreenHeight() - r.height() <= 150) {
            return false;
        }
        return true;
    }
}
